<?php
class Model_Garage extends Model_WotHub {
	const ACTIF = 1;
	const RESERVE = 0;

	public function getTanksAuGarage($account_id) {
		$sql = 'SELECT t.name, t.nation, t.image, t.tank_id, t.level, t.premium, s.*
                FROM tanks t
                JOIN garage g ON g.tank_id = t.tank_id
		        JOIN tankstats s ON s.tank_id = t.tank_id AND s.account_id = g.account_id
                WHERE g.account_id = :accountid
		        AND actif = :actif
		        ORDER BY t.level DESC';
		$bind = array(':accountid' => $account_id,
		              ':actif' => self::ACTIF);
		$garage = $this->_pdo->executerSql($sql, $bind);
		$actif = array();
		foreach ($garage as $tank) {
			$actif[$tank['tank_id']] = $tank;
		}
		return $actif;
	}
	
	public function getTanksEnReserve($account_id) {
		$sql = 'SELECT t.name, t.nation, t.image, t.tank_id, t.level, t.premium, s.*
                FROM tanks t
                JOIN garage g ON g.tank_id = t.tank_id
		        JOIN tankstats s ON s.tank_id = t.tank_id AND s.account_id = g.account_id
                WHERE g.account_id = :accountid
		        AND actif = :actif
		        ORDER BY t.level DESC';
		$bind = array(':accountid' => $account_id,
		              ':actif' => self::RESERVE);
		$garage = $this->_pdo->executerSql($sql, $bind);
		$reserve= array();
		foreach ($garage as $tank) {
			$reserve[$tank['tank_id']] = $tank;
		}
		return $reserve;
	}
	
	public function getGarageClan($membres, $filtre) {
	    $tanksIds = '';
	    if (is_array($filtre) === true && count($filtre) > 0) {
	        $filtre = ' AND t.level in ('.implode(',', $filtre).') ';
	    }
	    if (isset($_POST['type']) === true && count($_POST['type']) > 0 ) {
	        $filtre.= ' AND t.type IN (\''.implode("','", $_POST['type']).'\')';
	    }
	    if (isset($_POST['filtre']) === true) {
	        $tankIds = $this->executerSql("SELECT tank_id FROM filtres_tanks WHERE filtre_id = :filtreid", array(':filtreid' => $_POST['filtre']));
	        foreach($tankIds as $id) {
	            $tmp[] = $id['tank_id'];
	        }
	        $tanksIds = ' AND t.tank_id IN('.implode(',', $tmp).')';
	    }
	    $membres = implode(',', $membres);
	    $sql = 'SELECT t.name, t.nation, t.image, t.tank_id, t.level, COUNT( t.tank_id ) nb, t.premium
                FROM tanks t
                JOIN garage g ON g.tank_id = t.tank_id
                WHERE g.account_id IN('.$membres.')
		        AND actif = '.self::ACTIF.$filtre.$tanksIds.'
		        GROUP BY t.name, t.nation, t.image, t.tank_id, t.level, t.premium
		        ORDER BY t.level DESC, t.type, t.name';
	    $garage = $this->_pdo->executerSql($sql);
	    $actif = array();
	    foreach ($garage as $tank) {
	        $actif[$tank['tank_id']] = $tank;
	    }
	    return $actif;
	}
	
	public function getGarageClanMembre($membres, $filtre) {
	    if (is_array($filtre) === true && count($filtre) > 0) {
	        $filtre = ' AND t.level in ('.implode(',', $filtre).') ';
	    }
	    $filtremembres = implode(',', array_keys($membres));
	    $sql = 'SELECT t.name, t.nation, t.image, t.tank_id, t.level, g.account_id, t.premium
                FROM tanks t
                JOIN garage g ON g.tank_id = t.tank_id
                WHERE g.account_id IN('.$filtremembres.')
		        AND actif = '.self::ACTIF.$filtre.'
		        ORDER BY t.level DESC, t.type, t.name';
	    $garage = $this->_pdo->executerSql($sql);
	    $actif = array();
	    foreach ($garage as $membre) {
	        $pseudo = $membres[$membre['account_id']];
	        $actif[$pseudo][] = $membre;
	    }
	    return $actif;
	}
}
